﻿#include "revisechannelform.h"
#include "ui_revisechannelform.h"
#include <QJsonObject>
#include <QJsonDocument>
#include "httpclient.h"
#include <QMessageBox>
ReviseChannelForm::ReviseChannelForm(const AiChannelParam &param, const QString &ip, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ReviseChannelForm),
    m_param(param), m_ip(ip)
{
    ui->setupUi(this);
    this->initform();
}

ReviseChannelForm::~ReviseChannelForm()
{
    delete ui;
}

void ReviseChannelForm::initform()
{
    this->setWindowTitle("修改通道信息");
    ui->cbx_dev_type->setCurrentIndex(m_param.dev_type);
    ui->led_ip->setText(m_param.ip);
    ui->led_username->setText(m_param.username);
    ui->led_password->setText(m_param.password);
    ui->led_url->setText(m_param.url);
    ui->led_voice_ip->setText(m_param.voice_ip);
    ui->cbx_enable->setCurrentIndex(m_param.enable);
    ui->cbx_preview->setCurrentIndex(m_param.preview);

}

void ReviseChannelForm::on_btn_sure_clicked()
{
    QJsonObject json;
    json.insert("dev_type", ui->cbx_dev_type->currentIndex());
    json.insert("ip", ui->led_ip->text());
    json.insert("username", ui->led_username->text());
    json.insert("password", ui->led_password->text());
    json.insert("url", ui->led_url->text());
    json.insert("voice_ip", ui->led_voice_ip->text());
    json.insert("enable", ui->cbx_enable->currentIndex());
    json.insert("preview", ui->cbx_preview->currentIndex());
    json.insert("memo_fir", ui->led_memo_fir->text());
    json.insert("memo_sec", ui->led_memo_sec->text());
    json.insert("memo_thi", ui->led_memo_thi->text());
    QJsonDocument jsondoc;
    jsondoc.setObject(json);
    QString request = QString(jsondoc.toJson(QJsonDocument::Compact));
    QString request_path = QString("http://%1:8800/api/config/channel/%2").arg(m_ip).arg(m_param.id);
    HttpClient(request_path).success([this](const QString & response)
    {
        if(QMessageBox::information(nullptr, tr("通知"), tr("修改成功"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            emit sig_clear();
            this->close();
        }
    }).json(request).put();
}
